package com.sk.ccflower.lib.util.json;

import java.io.IOException;

import org.json.simple.parser.ContentHandler;
import org.json.simple.parser.ParseException;

/**
 * @author  Eunwook Lee
 */
public class KeyFinder implements ContentHandler {  
	
	private Object value;  
	private boolean found = false;  
	private boolean end = false;  
	private String key;  
	private String matchKey;          
	
	/**
	 * @param  matchKey
	 */
	public void setMatchKey(String matchKey){    
		this.matchKey = matchKey;  
	}         
	
	/**
	 * @return
	 */
	public Object getValue(){    
		return value;  
	}          
	
	/**
	 * @return
	 */
	public boolean isEnd(){    
		return end;  
	}          
	
	/**
	 * @param  found
	 */
	public void setFound(boolean found){    
		this.found = found;  
	}          
	
	/**
	 * @return
	 */
	public boolean isFound(){    
		return found;  
	}          
	
	public void startJSON() throws ParseException, IOException {    
		found = false;    end = false;  
	}  
	
	public void endJSON() throws ParseException, IOException {    
		end = true;  
	}  
	
	public boolean primitive(Object value) throws ParseException, IOException {    
		if(key != null){      
			if(key.equals(matchKey)){        
				found = true;        
				this.value = value;        
				key = null;        
				
				return false;      
			}    
		}    
		
		return true;  
	}  
	
	public boolean startArray() throws ParseException, IOException {    
		return true;  
	}          
	
	public boolean startObject() throws ParseException, IOException {    
		return true;  
	}  
	
	public boolean startObjectEntry(String key) throws ParseException, IOException {    
		this.key = key;    
		return true;  
	}          
	
	public boolean endArray() throws ParseException, IOException {    
		return false;  
	}  
	
	public boolean endObject() throws ParseException, IOException {    
		return true;  
	}  
	
	public boolean endObjectEntry() throws ParseException, IOException {    
		return true;  
	}
}